<?php
namespace common\component\exception;

use yii;
use yii\base\ErrorHandler as BaseErrorHandler;
use common\models\Error;
use common\component\tool\Tool;

class ErrorHandler extends BaseErrorHandler
{
    // public $errorView = '@app/views/errorHandler/error.php';
    /*错误、异常 收集*/
    public function renderException($exception)
    {
        // $error = Yii::$app->errorHandler;
        
        $url = Yii::$app->request->hostInfo . Yii::$app->request->getUrl();//当前完整URL
        $get = Yii::$app->request->get();
        $post = Yii::$app->request->post();
        // P($exception);

        $status_code = '';//错误码 比如404
        $obj_class = '';//错误的实例名
        $message = '';//错误消息
        $file = '';//错误文件
        $line = '';//错误行
        if(!empty($exception) and gettype($exception) == 'object'){//捕捉的错误异常
            $status_code = (string)(isset($exception->statusCode) ? $exception->statusCode : $exception->getCode());//错误码 比如404
            $obj_class = get_class($exception);//错误的实例名
            $message = $exception->getMessage();//错误消息
            $file = $exception->getFile();//错误文件
            $line = $exception->getLine();//错误行
        }

        $data = array(
            "status_code" => $status_code,//错误码 比如404
            "obj_class" => $obj_class,//错误的实例名
            "message" => $exception->getMessage(),//错误消息
            "file" => $file,//错误文件
            "line" => $line,//错误行
            "url" => $url,//当前完整URL
            "get_args" => !empty($get)?json_encode($get):"",//发生错误时候，GET参数
            "post_args" => !empty($post)?json_encode($post):"",//发生错误时候，POST参数
            "cookies" => json_encode($_COOKIE),//发生错误时候，cookies
            "session" => json_encode($_SESSION),//发生错误时候，session
            // "state" => 1,
            // "app_flag" => Yii::$app->params['appName'],
            "ip" => Yii::$app->request->userIP,
            "browser" => $_SERVER['HTTP_USER_AGENT'],
            // "solve_time" => '',
            "add_time" => date("Y-m-d H:i:s", time()),
        );
        // P($data);
        if(Error::add($data)){
            $serverName = Yii::$app->request->serverName;//获取域名,参考https://blog.csdn.net/weixin_35869703/article/details/112831167
            // $this->P($serverName);
            $module = Yii::$app->controller->module->id;//请求的模块名称
            // $this->P($module);
            if(($serverName != 'auth.console.uthinks.cn' 
            and $serverName != 'cms.console.uthinks.cn' 
            and $serverName != 'im.console.uthinks.cn') and $module != 'backend'){//如果是前端请求
                /*转到404页面 */
                header("HTTP/1.1 303 See Other");
                header('Location: /404.html');
            }else{
                echo Tool::showRes([-1, '程序错误!!!，请联系管理员，电话：13915028703（高）']);
                // echo 'GAME OVER!!!';
            }
        }else{
            echo 'error!!!';
        }
    }

    private function P($arr, $die = true)
    {
        echo "<pre>";
        print_r($arr);
        // var_dump($arr);
        echo "</pre>";
        if($die){
            die;
        }
    }
}